// express路由
const express = require('express')

const router = express.Router()

// 路由中间件
router.use((req, res, next) => {
  let d = new Date()
  let h = d.getHours()
  let m = d.getMinutes()
  let s = d.getSeconds()
  console.log('这是路由中间件1' + ' '+ h + ':' + m + ':' + s)
  next()
})
// 局部中间件
const mw1 = (req, res, next) => {
  let str = '这是局部中间件1'
  console.log(str)
  next()
}

// 挂载路由
router.get('/get', [mw1],(req, res) => {
  const query = req.query
  res.send({
    status: 200,
    message: 'get请求成功',
    data: query
  })
})
router.post('/post', (req, res) => {
  res.send('post请求成功')
})
router.delete('/delete', (req, res) => {
  res.send('delete请求成功')
})
router.get('/jsonp', (req, res) => {
  const funName = req.query.callback
  const data = {name: '王五', age: 56}
  const scriptStr = `${funName}(${JSON.stringify(data)})`
  res.send(scriptStr)
})

// 导出router
module.exports = router